<template>
	<view class="auth-main">
		<view class="auth-bottom">
			<text class="font-333-36" style="font-weight: 600;">登录授权</text>
			<text class="auth-btn" @click="handleAuth">一键登录</text>
			<text class="cancel-btn" @click="cancelAuth">取消授权</text>
			<view class="row-container-center" style="width: 100%;" @click="changeAgree">
				<image class="choose-ic" src="../static/ic_choose_not.png" v-if="!hasAgree"></image>
				<image class="choose-ic" src="../static/ic_chosen.png" v-if="hasAgree"></image>
				<text class="font-666-28">登录视为同意</text>
				<text class="font-333-28" @click.stop="gotoAgreement" style="flex-shrink: 0;">《最星城用户协议》</text>
				<text class="font-666-28">与</text>
				<text class="font-333-28" @click.stop="gotoPrivacy" style="flex-shrink: 0;">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"authWindow",
		data() {
			return {
				hasAgree:false,
				code:'',
				signature:'',
				rawData:'',
				encryptedData:'',
				iv:''
			};
		},
		
		onShow() {
			// uni.login({
				
			// })
		},
		
		methods:{
			changeAgree(){
				this.hasAgree = !this.hasAgree
			},
			
			handleAuth(){
				let that = this
				if (!this.hasAgree){
					uni.showToast({
						title:"请阅读并同意用户协议",
						icon:'none'
					})
					return
				}
				uni.login({
					success(loginRes) {
						that.code = loginRes.code
						let dataParams = {
							code:that.code
						}
						if (that.cookie.get("jx_inviteUserId")){
							that.$set(dataParams , "inviteUserId" ,that.cookie.get("jx_inviteUserId"))
						}
						console.log("jx_shareOrderId----" + that.cookie.get("jx_shareOrderId"))
						if (that.cookie.get("jx_shareOrderId")){
							that.$set(dataParams , "orderId" , that.cookie.get("jx_shareOrderId"))
						}
						
						that.http({
							url: "/common/wx/openId",
							method: "GET",
							data: dataParams,
							success: userRes  => {
								if (userRes.code === 200){
									that.openId = userRes.data
									that.cookie.set('jx_openId' , that.openId)
									that.handleLogin()
								} else if (userRes.code == 500){
									setTimeout(()=>{
										uni.showToast({
											title:userRes.msg,
											icon:'none'
										})
									} , 500)
								}
							},
							fail: err => {
								
							}
						})
					}
				})
			},
			
			// handleGetAuthUrl(){
			// 	if (!this.hasAgree){
			// 		uni.showToast({
			// 			title:"请阅读并同意用户协议",
			// 			icon:'none'
			// 		})
			// 		return
			// 	}
			// 	let that = this
			// 	uni.getUserProfile({
			// 		desc:'获取头像和昵称',
			// 		success(infoRes) {
			// 			console.log('getUserProfile------' , JSON.stringify(infoRes))
			// 			that.signature = infoRes.signature
			// 			that.rawData = infoRes.rawData
			// 			that.encryptedData = infoRes.encryptedData
			// 			that.iv = infoRes.iv
						
			// 			uni.login({
			// 				success(loginRes) {
			// 					that.code = loginRes.code
			// 					let dataParams = {
			// 						signature:that.signature,
			// 						rawData:that.rawData,
			// 						encryptedData:that.encryptedData,
			// 						iv:that.iv,
			// 						code:that.code
			// 					}
			// 					if (that.cookie.get("jx_inviteUserId")){
			// 						that.$set(dataParams , "inviteUserId" ,that.cookie.get("jx_inviteUserId"))
			// 					}
								
			// 					console.log("jx_shareOrderId----" + that.cookie.get("jx_shareOrderId"))
								
			// 					if (that.cookie.get("jx_shareOrderId")){
			// 						that.$set(dataParams , "orderId" , that.cookie.get("jx_shareOrderId"))
			// 					}
								
			// 					that.http({
			// 						url: "/common/info",
			// 						method: "GET",
			// 						data: dataParams,
			// 						success: userRes  => {
			// 							if (userRes.code === 200){
			// 								that.openId = userRes.data
			// 								that.cookie.set('jx_openId' , that.openId)
			// 								that.handleLogin()
			// 							} else if (userRes.code == 500){
			// 								that.handleGetAuthUrl()
			// 							}
			// 						},
			// 						fail: err => {
										
			// 						}
			// 					})
			// 				}
			// 			})
			// 		}
			// 	})
			// },
			
			handleLogin(){
				let that = this
				this.http({
					url:"/login/submit",
					method:"POST",
					data:{
						username: that.openId,
						password: that.openId,
					},
					success(res) {
						if (res.code == 200){
							that.cookie.set("jx_token" , res.data.token)
							that.cookie.set("jx_userInfo" , res.data)
							that.cookie.set('jx_userId' , res.data.userId)
							that.cookie.set('jx_mobile' , res.data.mobile)
							uni.$emit("authSuccess")
						}
					},
					fail(e) {
					}
				})
			},
			
			cancelAuth(){
				uni.$emit("authCancel")
			},
			
			gotoAgreement(){
				uni.navigateTo({
					url:'/pages/mine/agreement'
				})
			},
			
			gotoPrivacy(){
				uni.navigateTo({
					url:'/pages/mine/privacy'
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.auth-main{
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		background-color: rgba(0, 0, 0, 0.5);
		width: 100%;
		height: 100vh;
		z-index: 500;
		position: fixed;
		top: 0;
		flex: 1;
		
		.auth-bottom{
			display: flex;
			flex-direction: column;
			align-items: center;
			width: calc(100% - 60rpx);
			padding: 50rpx 30rpx;
			background-color: white;
			border-top-left-radius: 16rpx;
			border-top-right-radius: 16rpx;
		
			.auth-btn{
				width: 100%;
				height: 88rpx;
				text-align: center;
				line-height: 88rpx;
				background-color: #FFD100;
				border-radius: 10rpx;
				margin-top: 50rpx;
				font-size: 32rpx;
				color: #333333;
			}
			
			.cancel-btn{
				width: 100%;
				height: 88rpx;
				text-align: center;
				line-height: 88rpx;
				background-color: #F5F5F5;
				border-radius: 10rpx;
				margin-top: 20rpx;
				margin-bottom: 40rpx;
				font-size: 32rpx;
				color: #333333;
			}
			
			.choose-ic{
				width: 32rpx;
				height: 32rpx;
				margin-right: 10rpx;
			}
		}
	}
</style>